/*******************************************************************************
*  file    : modemmanager.hpp
*  created : 03.08.2013
*  author  : 
*******************************************************************************/

#ifndef MODEMMANAGER_HPP
#define MODEMMANAGER_HPP

#include <QObject>
#include <QTime>
#include <list>
#include <memory>
#include <utils.hpp>
#include <qatresult.h>
#include "archivedb.hpp"

typedef std::list<std::unique_ptr<MoleTask>> MoleTaskList;

class QTimer;
class GsmModem;
class GsmSmsReader;
class GsmSmsSender;

class ModemManager : public QObject
{
    Q_OBJECT
    explicit ModemManager(QObject *parent = 0);
public:
    static ModemManager* instance();
    ~ModemManager();
    QString        modemPort   () const;
    void           setModemPort(const QString& modemPort);
    const QString& modemStatus () const;

    int           sendInfoRequest(const QStringList& num_id);
    int           sendPosRequest (const QStringList& num_id);
    int           sendModeRequest(const QStringList& num_id, int mode);
    const MoleTaskList& taskList  ( ) const;

    int  modemBaudrate() const;
    void setModemBaudrate(int baudrate);

public slots:
    void start();

private slots:
    void onTime();
    void stop();

    void modemInitResult(bool);
    void messageCount   (int );
    void onModemError   (int );
private:
    void loadTaskList     ();
    void processIncoming  ();
    void requestModemMoney();
    void reconnectModem   ();

    void createModem      ();
    void deleteModem      ();
private:
    QTimer* timer_;

    QString       modemPort_;
    int           modemBaudrate_;
    GsmModem*     modem_;
    GsmSmsReader* sms_reader_;
    GsmSmsSender* sms_sender_;

    QString       modem_status_;
    QString       modem_money_;
    QTime         last_account_request_;

private: // отправка SMS
    bool is_sending_;
    int  sms_count_;
    MoleTaskList task_list_;

    std::vector<MoleLine>     moles_ ;
    std::vector<OutgoingLine> nonsnd_;
private:
    void startSending();
private slots:
    void smsSended(const QString& id, int result);
    void ussd(const QString& );
    void phoneBookEntry(const QString&);
};

#endif // MODEMMANAGER_HPP
